package com.cnb.util;

/** 
 * @author chennb 
 * Creation date: 02-23-2010
 * 时间处理类
 * 
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class DateUtil {
	private static Logger log = Logger.getLogger(DateUtil.class);

	public static String dateDisplay(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date d = new Date(time);
		return format.format(d);
	}

	/**
	 * 日期转换：指定的日期转换为形如yyyy-mm-dd hh:mm的格式
	 * 
	 * @param d
	 *            需要显示的日期
	 * @return
	 */
	public static String dateDisplay(Date d) {
		if (null != d) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			return format.format(d);
		} else {
			return "";
		}
	}

	/**
	 * 日期转换：指定的日期转换为形如yyyy-mm-dd的格式
	 * 
	 * @param d
	 *            需要显示的日期
	 * @return
	 */
	public static String dateDisplayToYMD(Date d) {
		if (null != d) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			return format.format(d);
		} else {
			return "";
		}
	}

	/**
	 * 日期转换：指定的日期转换为形如yyyy-mm的格式
	 * 
	 * @param d
	 *            需要显示的日期
	 * @return
	 */
	public static String dateDisplayToYM(Date d) {
		if (null != d) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
			return format.format(d);
		} else {
			return "";
		}
	}

	/**
	 * 日期转换：指定的日期转换为形如yyyymmdd的格式
	 * 
	 * @param d
	 *            需要显示的日期
	 * @return
	 */
	public static String dateDisplayToYyyymmdd(Date d) {
		if (null != d) {
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			return format.format(d);
		} else {
			return "";
		}
	}
	/**
	 * 日期转换：指定的日期转换为formatStr格式
	 * @param d
	 * @param formatStr
	 * @return
	 */
	public static String dateDisplayToFormat(Date d,String formatStr) {
		if (null != d) {
			SimpleDateFormat format = new SimpleDateFormat(formatStr);
			return format.format(d);
		} else {
			return "";
		}
	}

	/**
	 * 当前日期转换为“yyyy-mm-dd”的格式
	 * 
	 * @return
	 */
	public static String dateDisplay() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		return format.format(new Date());
	}

	/**
	 * 获得时间
	 * 
	 * @param key
	 * @return
	 */
	public static String getTime(String key) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		try {

			return String.valueOf(format.parse(key).getTime());
		} catch (ParseException e) {
			log.info("DateUtil->getTime():" + e.toString());
			return "0";
		}
	}

	/**
	 * 获得指定格式的日期
	 * 
	 * @param key
	 * @return
	 */
	public static String getDisplayTime(String key) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			Date date = new Date(Long.parseLong(key));
			return format.format(date);
		} catch (Exception e) {
			log.info("DateUtil->getDisplayTime(String key):" + e.toString());
			return "0";
		}
	}

	/**
	 * 获得指定格式的日期
	 * 
	 * @param key
	 * @return
	 */
	public static Date getDate(String key) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// Date date = new Date(Long.parseLong(key));
			// Date date = format.parse(key);
			return format.parse(key);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("DateUtil->getDate(String key):" + e.toString());
			return null;
		}
	}

	/**
	 * 获得指定格式的日期
	 * 
	 * @param key
	 * @return
	 */
	public static Date getDate1(String key, String formatString) {
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		try {
			// Date date = new Date(Long.parseLong(key));
			// Date date = format.parse(key);
			return format.parse(key);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("DateUtil->getDate(String key):" + e.toString());
			return null;
		}
	}

	/**
	 * 将字符型日期加时间转成日期加时间类型
	 * 
	 * @param key
	 *            String
	 * @return Date
	 */
	public static Date getDateAndTime(String key) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			// Date date = new Date(Long.parseLong(key));
			// Date date = format.parse(key);
			return format.parse(key);
		} catch (Exception e) {
			log.info("DateUtil->getDateAndTime(String key):" + e.toString());
			return null;
		}

	}

	/**
	 * 将字符型日期加时间转成日期加时间类型
	 * 
	 * @param key
	 *            String
	 * @return Date
	 */
	public static Date getXMLDateAndTime(String key) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		try {
			// Date date = new Date(Long.parseLong(key));
			// Date date = format.parse(key);
			return format.parse(key);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("DateUtil->getDateAndTime(String key):" + e.toString());
			return null;
		}

	}

	/**
	 * 根据日历字段计算结束日期
	 * 
	 * @param beginDate 起始日期
	 * @param field 字段
	 * @param amount 增加值
	 * @return
	 * @throws ParseException
	 */
	public static Date add(Date beginDate, int field, int amount)
			throws ParseException {
		Date endDate = null;
		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calender = Calendar.getInstance();
		calender.setTime(beginDate);
		calender.add(field, amount);
		endDate = dft.parse(dft.format(calender.getTime()));
		return endDate;
	}
	
	public static int betweenDays(Date beginDate,Date endDate){
		long days=(endDate.getTime()-beginDate.getTime())/(1000*3600*24);
		return (int)days;
	}

	/**
	 * 将日期加时间类型转成字符串日期加时间类型
	 * 
	 * @param key
	 *            String
	 * @return Date
	 */
	public static String getXMLDateAndTimeStr(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return format.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("DateUtil->getXMLDateAndTimeStr(Date date):"
					+ e.toString());
			return null;
		}
	}

	public static Date getXMLStrDateAndTime(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			log.info("DateUtil->getXMLStrDateAndTime(String date):"
					+ e.toString());
			return null;
		}
	}

	/**
	 * 获得指定格式的日期时间格式
	 * 
	 * @param time
	 * @return
	 */
	public static String getTime(long time) {
		StringBuffer str = new StringBuffer();
		int hour = (int) time / (60 * 60 * 1000);
		int minute = (int) (time % (60 * 60 * 1000)) / (60 * 1000);
		int second = (int) (time - hour * (60 * 60 * 1000) - minute
				* (60 * 1000)) / 1000;

		if (hour > 0) {
			str.append(hour + "小时:");
		}
		if (minute > 0) {
			str.append(minute + "分种");
		}
		if (second >= 0) {
			str.append(second + "秒");
		}
		return str.toString();
	}

	/**
	 * 将dd/mm/yyyy类型的字符串转化成yyyy-mm-dd
	 * 
	 * @param value
	 *            String
	 * @return String
	 */
	public static String getDateByString(String value) {
		if (value == null || "".equals(value)) {
			return value;
		}
		value = value.substring(0, 10);
		String[] date = value.split("/");
		String mm = "";
		String dd = "";
		String yyyy = date[2].trim().substring(0, 4);
		if (date[0].trim().length() < 2) {
			mm = "0" + date[0].trim();
		} else {
			mm = date[0].trim();
		}
		if (date[1].trim().length() < 2) {
			dd = "0" + date[1].trim();
		} else {
			dd = date[1].trim();
		}
		value = yyyy + "-" + mm + "-" + dd;
		return value;
	}

	/**
	 * 获得带毫秒的字符串
	 * 
	 * @param time
	 *            long
	 * @return String
	 */
	public static String getDateString() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S");
		return format.format(new Date());
	}

	public final static String svDatePatten = "yyyy-MM-dd";
	public final static String svDateTimePatten = "yyyy-MM-dd HH:mm";
	public final static String svLongDatePatten = "yyyy-MM-dd";
	public final static String svLongDateTimePatten = "yyyy-MM-dd HH:mm";
	public final static String svDateTimeFilePattern = "yyyy-MM-dd HHmmss ";
	public final static String svDateTimesFilePattern = "yyyyMMddhhmmss";
	public final static String svDateFilePattern = "yyyyMMdd";

	private final static SimpleDateFormat mvDateFormatter = new SimpleDateFormat(
			svDatePatten);
	private final static SimpleDateFormat mvDateTimeFormatter = new SimpleDateFormat(
			svDateTimePatten);
	private final static SimpleDateFormat mvLongDateFormatter = new SimpleDateFormat(
			svLongDatePatten);
	private final static SimpleDateFormat mvLongDateTimeFormatter = new SimpleDateFormat(
			svLongDateTimePatten);
	private final static SimpleDateFormat mvLongDateTimesFormatter = new SimpleDateFormat(
			svDateTimesFilePattern);

	private final static SimpleDateFormat mvDatesFormatter = new SimpleDateFormat(
			svDateFilePattern);

	/** ***Date function start** */
	public static java.util.Date getCurrentDate() {
		return new java.util.Date(System.currentTimeMillis());
	}

	// public static java.util.Date getYesterday(){
	// return rollDay(getCurrentDate(),-1);
	// }
	//
	// public static java.util.Date getTomorrow(){
	// return rollDay(getCurrentDate(),1);
	// }
	//
	// private static java.util.Date rollDay(java.util.Date pDate,int pDay){
	// java.util.Calendar lvCal = java.util.Calendar.getInstance();
	// lvCal.setTime(pDate);
	// lvCal.add(lvCal.HOUR, pDay*24);
	// lvCal.set(lvCal.HOUR, 0);
	// lvCal.set(lvCal.MINUTE, 0);
	// lvCal.set(lvCal.SECOND, 0);
	// lvCal.set(lvCal.MILLISECOND,0);
	// return lvCal.getTime();
	// }

	public static java.sql.Timestamp getCurrentTimeStamp() {
		return new java.sql.Timestamp(System.currentTimeMillis());
	}

	public static String formatDates(java.util.Date p1) {
		return mvDatesFormatter.format(p1);
	}

	public static String formatDateTimes(java.util.Date p1) {
		return mvLongDateTimesFormatter.format(p1);
	}
	/**
	 * 获取当前时间yyyyMMddhhmmss
	 * @return
	 */
	public static String getCurrentDateTime() {
		Date date=new Date();
		return mvLongDateTimesFormatter.format(date);
	}

	public static String formatDateTime(java.util.Date p1) {
		if (p1 != null)
			return mvDateTimeFormatter.format(p1);
		else
			return null;
	}

	public static String formatDateTime(java.sql.Date p1) {
		if (p1 != null) {
			java.util.Date lvDate = new java.util.Date(p1.getTime());
			return mvDateTimeFormatter.format(lvDate);
		} else
			return null;
	}

	public static String formatLongDate(java.util.Date p1) {
		if (p1 != null)
			return mvLongDateFormatter.format(p1);
		else
			return null;
	}

	public static String formatLongDateTime(java.util.Date p1) {
		if (p1 != null)
			return mvLongDateTimeFormatter.format(p1);
		else
			return null;
	}

	public static java.util.Date parseDate(String p1)
			throws java.text.ParseException {
		return mvDateFormatter.parse(p1);
	}

	public static java.sql.Date parseSqlDate(String p1)
			throws java.text.ParseException {
		return new java.sql.Date(mvDateFormatter.parse(p1).getTime());
	}

	public static java.util.Date parseDateTime(String p1)
			throws java.text.ParseException {
		return mvDateTimeFormatter.parse(p1);
	}

	public static java.sql.Timestamp parseSqlTime(String p1)
			throws java.text.ParseException {
		return new java.sql.Timestamp(mvDateTimeFormatter.parse(p1).getTime());
	}
	/**
	 * 计算两者时间差
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static String sub(Date d1,Date d2){
		
		long diff = d1.getTime() - d2.getTime();

		long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);
        String str=diffDays + " 天, "+diffHours + " 小时, "+diffMinutes + " 分钟, "+diffSeconds + " 秒.";
  
        return str;

		
	}

	public static java.util.Date convertSqlToUtilDate(java.sql.Date p1) {
		return new java.util.Date(p1.getTime());
	}

	public static java.sql.Date convertUtilToSqlDate(java.util.Date p1) {
		return new java.sql.Date(p1.getTime());
	}

	public static int getYear() {
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		return year;
	}
}
